package top.healthylife.service.impl;/*
 *@Author GaoZeXi
 *@Created time 2020/4/2 1:04
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.healthylife.domain.Permission;
import top.healthylife.domain.Role;
import top.healthylife.mapper.PermissionMapper;
import top.healthylife.mapper.RoleMapper;
import top.healthylife.service.UserService;
import top.healthylife.domain.User;
import top.healthylife.mapper.UserMapper;

import java.util.Set;

//注册到服务中心
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;
    /**
     * 根据用户名查询用户具体的角色信息,以及对应的权限信息
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
       User user = userMapper.findByUsername(username);
       if(user ==null){
            return null;
       }
        Integer userId = user.getId();
        Set<Role> roles = roleMapper.findByUserId(userId);
        if(roles==null){
            return user;
        }
        user.setRoles(roles);
        for (Role role : roles) {
            Integer id = role.getId();
           Set<Permission> permissions = permissionMapper.findByRoleId(id);
           if(permissions==null){
               return user;
           }
           role.setPermissions(permissions);
        }
        return user;
    }


}
